#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <sstream>
#include "metod.h"
#include "secondary_functions.h"
using namespace std;




void algoritm(int A[3][3], int hod){
    int B[3][3] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    //первый ход
    if(hod == 1){
        if(A[1][1] == 1){ //центр
            A[0][0] = 2;
        }else{ //любой исход, кроме центра
            A[1][1] = 2;
        }
    }

    //второй ход
    if(hod == 2) {
        if (A[1][1] == 2) { // Центр программы

            if (A[2][0] == 1 && A[0][2] == 1){
                A[0][0] = 2;
            }
            if (A[0][0] == 1 && A[2][2] == 1){
                A[0][2] = 2;
            }
            if (A[2][0] == 1 && A[0][2] == 1){
                A[0][0] = 2;
            }

            if (A[0][0] == 1) {
                if (A[1][0] == 1) { A[2][0] = 2; return; }
                if (A[2][0] == 1) { A[1][0] = 2; return; }
                if (A[0][1] == 1) { A[0][2] = 2; return; }
                if (A[0][2] == 1) { A[0][1] = 2; return; }
            }
            if (A[0][2] == 1) {
                if (A[0][0] == 1) { A[0][1] = 2; return; }
                if (A[0][1] == 1) { A[0][0] = 2; return; }
                if (A[1][2] == 1) { A[2][2] = 2; return; }
                if (A[2][2] == 1) { A[1][2] = 2; return; }
            }
            if (A[2][2] == 1) {
                if (A[0][2] == 1) { A[1][2] = 2; return; }
                if (A[1][2] == 1) { A[0][2] = 2; return; }
                if (A[2][1] == 1) { A[2][0] = 2; return; }
                if (A[2][0] == 1) { A[2][1] = 2; return; }
            }
            if (A[2][0] == 1) {
                if (A[2][1] == 1) { A[2][2] = 2; return; }
                if (A[2][2] == 1) { A[2][1] = 2; return; }
                if (A[1][0] == 1) { A[0][0] = 2; return; }
                if (A[0][0] == 1) { A[1][0] = 2; return; }
            }
        }

        if (A[1][1] == 2) { //центр программы, игрок в центральных
            //игрок ставит первым ходом боковой центр, вторым левая
            if (A[0][1] == 1 && A[0][2] == 1){ A[0][0] = 2; return; }
            if (A[1][2] == 1 && A[2][2] == 1){ A[0][2] = 2; return; }
            if (A[2][1] == 1 && A[2][0] == 1){ A[2][2] = 2; return; }
            if (A[1][0] == 1 && A[0][0] == 1){ A[2][0] = 2; return; }

            if (A[0][1] == 1) {
                if (A[0][0] == 0) { A[0][0] = 2; return;}
                //else { A[0][2] = 2; return;}
            }
            if (A[1][2] == 1) {
                if (A[0][2] == 0) { A[0][2] = 2; return;}
                else { A[2][2] = 2; return;}
            }
            if (A[2][1] == 1) {
                if (A[2][2] == 0) { A[2][2] = 2; return;}
                else { A[2][0] = 2; return;}
            }
            if (A[1][0] == 1) {
                if (A[2][0] == 0) { A[2][0] = 2; return;}
                else { A[0][0] = 2; return;}
            }


        }

        if (A[1][1] == 1) { // Центр игрока
            if (A[0][1] == 1) { A[2][1] = 2; return; }
            if (A[0][2] == 1) { A[2][0] = 2; return; }
            if (A[1][2] == 1) { A[1][0] = 2; return; }
            if (A[2][2] == 1) { A[0][2] = 2; return; }
            if (A[2][1] == 1) { A[0][1] = 2; return; }
            if (A[2][0] == 1) { A[0][2] = 2; return; }
            if (A[1][0] == 1) { A[1][2] = 2; return; }

        }
    }


    if(hod == 3) {
        if (A[1][1] == 1) { //центр игрока
            //впервую очередь не даём занять соседние углы
            if (A[0][0] == 1){
                if (A[0][2] == 1) { A[0][1] = 2; }
                if (A[2][0] == 1) { A[1][0] = 2; }
            }
            if (A[0][2] == 1){
                if (A[2][2] == 1) { A[1][2] = 2; }
                if (A[0][0] == 1) { A[0][1] = 2; }
            }
            if (A[2][2] == 1){
                if (A[0][2] == 1) { A[1][2] = 2; }
                if (A[2][0] == 1) { A[2][1] = 2; }
            }
            if (A[2][0] == 1){
                if (A[0][0] == 1) { A[1][0] = 2; }
                if (A[2][2] == 1) { A[2][1] = 2; }
            }

            //потом сейвим прямые диагонали
            if (A[0][1] == 1){ if (A[2][1] == 0) { A[2][1] = 2; } }
            if (A[1][0] == 1){ if (A[1][2] == 0) { A[1][2] = 2; } }
            if (A[1][2] == 1){ if (A[1][0] == 0) { A[1][0] = 2; } }
            if (A[2][1] == 1){ if (A[0][1] == 0) { A[0][1] = 2; } }

            //косые диагонали
            if (A[0][2] == 1){ if (A[2][0] == 0) { A[2][0] = 2; } }
            if (A[2][0] == 1){ if (A[0][2] == 0) { A[0][2] = 2; } }
            if (A[0][0] == 1){ if (A[2][2] == 0) { A[2][2] = 2; } }
            if (A[2][2] == 1){ if (A[0][2] == 0) { A[0][2] = 2; } }

            //else { rand_pos(A); return; }
        }

        if (A[1][1] == 2) { //центр прога

            if (A[0][0] == 2 && A[0][1] == 1 && A[1][2] == 1 && A[2][2] == 1 && A[0][2] == 0){ A[0][2] = 2; return; }
            if (A[0][2] == 2 && A[1][2] == 1 && A[2][1] == 1 && A[2][0] == 1 && A[2][2] == 0){ A[2][2] = 2; return; }
            if (A[2][2] == 2 && A[2][1] == 1 && A[1][0] == 1 && A[0][0] == 1 && A[2][0] == 0){ A[2][0] = 2; return; }
            if (A[2][0] == 2 && A[1][0] == 1 && A[0][1] == 1 && A[0][2] == 1 && A[0][0] == 0){ A[0][0] = 2; return; }

            if (A[0][0] == 1 && A[1][0] == 1 && A[0][2] == 1 && A[0][1] == 0){ A[0][1] = 2; return; }
            if (A[0][1] == 1 && A[0][2] == 1 && A[2][2] == 1 && A[1][2] == 0){ A[1][2] = 2; return; }
            if (A[1][2] == 1 && A[2][2] == 1 && A[2][0] == 1 && A[2][1] == 0){ A[2][1] = 2; return; }
            if (A[0][0] == 1 && A[2][0] == 1 && A[2][1] == 1 && A[1][0] == 0){ A[1][0] = 2; return; }

            if (A[0][0] == 1 && A[0][1] == 1 && A[2][0] == 1 && A[1][0] == 0){ A[1][0] = 2; return; }
            if (A[0][0] == 1 && A[0][2] == 1 && A[1][2] == 1 && A[0][1] == 0){ A[0][1] = 2; return; }
            if (A[0][2] == 1 && A[2][2] == 1 && A[2][1] == 1 && A[1][2] == 0){ A[1][2] = 2; return; }
            if (A[1][0] == 1 && A[2][0] == 1 && A[2][2] == 1 && A[2][1] == 0){ A[2][1] = 2; return; }

            if (A[0][0] == 1 && A[0][2] == 1 && A[2][1] == 1 && A[0][1] == 0){ A[0][1] = 2; return; }
            if (A[0][2] == 1 && A[2][2] == 1 && A[1][0] == 1 && A[1][2] == 0){ A[1][2] = 2; return; }
            if (A[0][1] == 1 && A[2][0] == 1 && A[2][2] == 1 && A[2][1] == 0){ A[2][1] = 2; return; }
            if (A[0][0] == 1 && A[2][0] == 1 && A[1][2] == 1 && A[1][0] == 0){ A[1][0] = 2; return; }

            if (A[1][2] == 2 && A[1][0] == 1 && A[0][2] == 1 && A[2][2] == 1){ A[2][1] = 2; return; }
            if (A[2][1] == 2 && A[0][1] == 1 && A[2][2] == 1 && A[2][0] == 1){ A[1][0] = 2; return; }
            if (A[1][0] == 2 && A[0][0] == 1 && A[2][0] == 1 && A[1][2] == 1){ A[0][1] = 2; return; }
            if (A[0][1] == 2 && A[2][1] == 1 && A[0][0] == 1 && A[0][2] == 1){ A[1][2] = 2; return; }

            if (A[1][0] == 1 && A[2][0] == 1 && A[2][1] == 1) {
                if (A[2][2] == 0) { A[2][2] = 2; }
                if (A[0][0] == 0) { A[0][0] = 2; }
                return;
            }
            if (A[1][0] == 1 && A[0][0] == 1 && A[0][1] == 1) {
                if (A[0][2] == 0) { A[0][2] = 2; }
                if (A[2][0] == 0) { A[2][0] = 2; }
                return;
            }
            if (A[0][1] == 1 && A[0][2] == 1 && A[1][2] == 1){
                if (A[0][0] == 0) { A[0][0] = 2; }
                if (A[2][2] == 0) { A[2][2] = 2; }
                return;
            }
            if (A[1][2] == 1 && A[2][2] == 1 && A[2][1] == 1) {
                if (A[2][0] == 0) { A[2][0] = 2; }
                if (A[0][2] == 0) { A[0][2] = 2; }
                return;
            }
            //проверка на победу через центр
            //прямые
            if (A[0][1] == 2 && A[2][1] == 0){ A[2][1] = 2; return; }
            if (A[2][1] == 2 && A[0][1] == 0){ A[0][1] = 2; return; }
            if (A[1][1] == 2 && A[1][2] == 0){ A[1][2] = 2; return; }
            if (A[1][2] == 2 && A[1][0] == 0){ A[1][0] = 2; return; }
            //диагональные
            if (A[0][0] == 2 && A[2][2] == 0){ A[2][2] = 2; return; }
            if (A[2][2] == 2 && A[0][0] == 0){ A[0][0] = 2; return; }
            if (A[2][0] == 2 && A[0][2] == 0){ A[0][2] = 2; return; }
            if (A[0][2] == 2 && A[2][0] == 0){ A[2][0] = 2; return; }

            //else { rand_pos(A); return; }

        }
    }

    if(hod == 4) {
        if (A[1][1] == 2){ //центр программы

            if (A[0][2] == 2 && A[1][0] == 2 && A[0][0] == 1 && A[1][2] == 1 && A[2][0] == 1 && A[2][2] == 1 && A[2][1] == 0){ A[2][1] = 2; return; }
            if (A[0][1] == 2 && A[2][2] == 2 && A[0][0] == 1 && A[0][2] == 1 && A[2][0] == 1 && A[2][1] == 1 && A[1][0] == 0){ A[1][0] = 2; return; }
            if (A[2][0] == 2 && A[1][2] == 2 && A[0][0] == 1 && A[0][2] == 1 && A[1][0] == 1 && A[2][2] == 1 && A[0][1] == 0){ A[0][1] = 2; return; }
            if (A[0][0] == 2 && A[2][1] == 2 && A[0][1] == 1 && A[0][2] == 1 && A[2][2] == 1 && A[2][0] == 1 && A[1][2] == 0){ A[1][2] = 2; return; }

            if (A[2][1] == 2){
                if (A[0][0] == 2 && A[0][2] == 0) { A[0][2] = 2; return; }
                if (A[0][2] == 2 && A[0][0] == 0) { A[0][0] = 2; return; }
                //else { rand_pos(A); return; }
            }
            if (A[1][0] == 2){
                if (A[0][2] == 2 && A[2][2] == 0) { A[2][2] = 2; return; }
                if (A[2][2] == 2 && A[0][2] == 0) { A[0][2] = 2; return; }
                //else { rand_pos(A); return; }
            }
            if (A[0][1] == 2){
                if (A[2][0] == 2 && A[2][2] == 0) { A[2][2] = 2; return; }
                if (A[2][2] == 2 && A[2][0] == 0) { A[2][0] = 2; return; }
                //else { rand_pos(A); return; }
            }
            if (A[1][2] == 2){
                if (A[0][0] == 2 && A[2][0] == 0) { A[2][0] = 2; return; }
                if (A[2][0] == 2 && A[0][0] == 0) { A[0][0] = 2; return; }
                //else { rand_pos(A); return; }
            }

            if (A[1][2] == 2 && A[1][0] == 1 && A[0][2] == 1 && A[2][2] == 1 && A[2][1] == 2) {if (A[0][1] == 1) {A[0][0] = 2; return;} else {A[0][1] = 2; return;}}
            if (A[2][1] == 2 && A[0][1] == 1 && A[2][2] == 1 && A[2][0] == 1 && A[1][0] == 2) {if (A[1][2] == 1) {A[0][2] = 2; return;} else {A[1][2] = 2; return;}}
            if (A[1][0] == 2 && A[0][0] == 1 && A[2][0] == 1 && A[1][2] == 1 && A[0][1] == 2) {if (A[2][1] == 1) {A[2][2] = 2; return;} else {A[2][1] = 2; return;}}
            if (A[0][1] == 2 && A[2][1] == 1 && A[0][0] == 1 && A[0][2] == 1 && A[1][2] == 2) {if (A[1][0] == 1) {A[2][0] = 2; return;} else {A[1][0] = 2; return;}}

            if (A[0][0] == 2 && A[0][2] == 2 && A[0][1] == 1 && A[1][2] == 1 && A[2][2] == 1 && A[2][0] == 1 && A[2][1] == 0){ A[2][1] = 2; return; }
            if (A[0][2] == 2 && A[2][2] == 2 && A[0][0] == 1 && A[1][2] == 1 && A[2][0] == 1 && A[2][1] == 1 && A[1][0] == 0){ A[1][0] = 2; return; }
            if (A[2][2] == 2 && A[2][0] == 2 && A[0][0] == 1 && A[0][2] == 1 && A[1][0] == 1 && A[2][1] == 1 && A[0][1] == 0){ A[0][1] = 2; return; }
            if (A[0][0] == 2 && A[2][0] == 2 && A[0][1] == 1 && A[0][2] == 1 && A[1][0] == 1 && A[2][2] == 1 && A[1][2] == 0){ A[1][2] = 2; return; }

            else { rand_pos(A); return; }

        }
        if (A[1][1] == 1){ //центр игрок
            if (A[0][0] == 2 && A[0][2] == 2 && A[2][1] == 2 && A[0][1] == 1 && A[2][0] == 1 && A[2][2] == 1){ rand_pos(A); return; }
            if (A[0][2] == 2 && A[2][2] == 2 && A[1][0] == 2 && A[0][0] == 1 && A[1][2] == 1 && A[2][0] == 1){ rand_pos(A); return; }
            if (A[0][1] == 2 && A[2][0] == 2 && A[2][2] == 2 && A[0][0] == 1 && A[0][2] == 1 && A[2][1] == 1){ rand_pos(A); return; }
            if (A[2][0] == 2 && A[0][0] == 2 && A[1][2] == 2 && A[1][0] == 1 && A[0][2] == 1 && A[2][2] == 1){ rand_pos(A); return; }

            if (A[0][0] == 2 && A[1][0] == 2 && A[2][1] == 2 && A[0][1] == 1 && A[1][2] == 1 && A[2][0] == 1 && A[0][2] == 0){ A[0][2] = 2; return; }
            if (A[0][1] == 2 && A[0][2] == 2 && A[1][0] == 2 && A[0][0] == 1 && A[1][2] == 1 && A[2][1] == 1 && A[2][2] == 0){ A[2][2] = 2; return; }
            if (A[0][1] == 2 && A[1][2] == 2 && A[2][2] == 2 && A[0][2] == 1 && A[1][0] == 1 && A[2][1] == 1 && A[2][0] == 0){ A[2][0] = 2; return; }
            if (A[2][0] == 2 && A[2][1] == 2 && A[1][2] == 2 && A[0][1] == 1 && A[1][0] == 1 && A[2][2] == 1 && A[0][0] == 0){ A[0][0] = 2; return; }

            else { rand_pos(A); return; }
        }
    }
}

